<?php
namespace jy_merch;
defined('BY_JYA') or exit('error');
/*
	招商员佣金计算
	
*/
class commission extends \plugin{
	/*
		计算抽成,$add是否创建抽成订单
	*/
	function get_choufee($order,$add=false,$merchid=0,$isup=0){
		if(is_array($order)){
			$order_id=$order['id'];
		}else{
			$order_id=$order;
		}
		$merchorder=pdo_get('jy_merch_order',array('uniacid'=>$_SESSION['uniacid'],'order_id'=>$order_id));
		if($merchorder['id']){
			$this->up_orderstatus($order_id,$merchid);
			if(!$isup || $order['clearstatus']){
				return $merchorder;
			}
		}
		if(!is_array($order)){
			$order=pdo_get('jy_weishop_order',array('uniacid'=>$_SESSION['uniacid'],'id'=>$order_id));
		}
		if($order['status']==0 || $order['status']==-1 || $order['status']==4){
			return $merchorder;
		}
		$choufee_set=$this->setting;
		$merchsfee=array();
		if($order['merchid'] && (!$merchid || $order['merchid']==$merchid)){
				$merch=array();
				if($choufee_set['commission_type']==3){
					$merch=pdo_get('jy_merch_user',array('uniacid'=>$_SESSION['uniacid'],'id'=>$order['merchid']),array('id','choufee_set'));
				}
				if($order['buytype']==4){
					$order['price']=0;
				}
				$order_goods=pdo_getall('jy_weishop_order_goods',array('uniacid'=>$_SESSION['uniacid'],'orderid'=>$order['id']),array('api_id','goodsid','optionid','price','total','total_price','goodssn','productsn','goodsinfo'));
				$costprice=0;
				foreach($order_goods as $ov){
					$total_price=$ov['price']*$ov['total'];
					if($total_price!=$ov['total_price']){
						pdo_update('jy_weishop_order_goods',array('total_price'=>$total_price),array('uniacid'=>$_SESSION['uniacid'],'id'=>$ov['id']));
					}
					$ov['goodsinfo']=$ov['goodsinfo']?json_decode($ov['goodsinfo'],true):array();
					$costprice +=$ov['goodsinfo']['costprice']*$ov['total'];
				}
				$goodsfee=pdo_getsum('jy_weishop_order_goods',array('uniacid'=>$_SESSION['uniacid'],'orderid'=>$order['id']),'total_price');
				if($order['peisong_id'] && $merchorder['sys_peiprice']<=0){
					$sys_peiprice=model('jy_weishop/order/dispatch')->get_merchpeisongfee($order);
				}
				$merchsfee[]=array(
					'id'=>$order['merchid'],
					'orderid'=>$order['id'],
					'choufee_set'=>$merch['choufee_set']?json_decode($merch['choufee_set'],true):array(),
					'orderfee'=>$order['price']-$order['out_upfee']-$order['dispatchprice'],
					'goodsfee'=>$goodsfee,
					'out_upfee'=>$order['out_upfee'],
					'dispatchprice'=>$sys_peiprice>0?$sys_peiprice:$order['dispatchprice'],
					'costprice'=>$costprice,
				);
		}
		$merchnum=count($merchsfee);
		foreach($merchsfee as &$mv){
			//$mv['dispatchprice']=$mv['dispatchprice']*1/$merchnum;
			$commission_totalfee=0;
			if($order['isdish']){
				$commission_type=$choufee_set['commission_type'];
				if($commission_type==3 && $mv['choufee_set'] && ($mv['choufee_set']['rate'] || $mv['choufee_set']['rateyuan'] || count($mv['choufee_set']['ratejieti'])>0)){
					$choufee_set=$mv['choufee_set'];
					$commission_type=$choufee_set['commission_type'];
				}
				if(strpos($choufee_set['dish_feetype'],';0;')!== false){
					$commission_totalfee +=$mv['orderfee'];
				}elseif(strpos($choufee_set['dish_feetype'],';4;')!== false){
					$commission_totalfee +=$mv['goodsfee'];
				}
				if(strpos($choufee_set['dish_feetype'],';1;')!== false){
					$commission_totalfee +=$mv['fanhefee'];
				}
				if(strpos($choufee_set['dish_feetype'],';2;')!== false){
					$commission_totalfee +=$mv['out_upfee'];
				}
				if(strpos($choufee_set['dish_feetype'],';3;')!== false){
					$commission_totalfee +=$mv['dispatchprice'];
				}
			}else{
				$commission_type=$choufee_set['shopcommission_type'];
				if($commission_type==3 && $mv['choufee_set'] && ($mv['choufee_set']['shoprate'] || $mv['choufee_set']['shoprateyuan'] || count($mv['choufee_set']['shopratejieti'])>0)){
					$choufee_set=$mv['choufee_set'];
					$commission_type=$choufee_set['shopcommission_type'];
				}
				if(strpos($choufee_set['shop_feetype'],';0;')!== false){
					$commission_totalfee +=$mv['orderfee'];
				}elseif(strpos($choufee_set['dish_feetype'],';4;')!== false){
					$commission_totalfee +=$mv['goodsfee'];
				}
				if(strpos($choufee_set['shop_feetype'],';3;')!== false){
					$commission_totalfee +=$mv['dispatchprice'];
				}
				$choufee_set['rate']=$choufee_set['shoprate'];
				$choufee_set['rateyuan']=$choufee_set['shoprateyuan'];
				$choufee_set['ratejieti']=$choufee_set['shopratejieti'];
			}
			//$commission_totalfee=$commission_totalfee?:$mv['goodsfee'];
			if($commission_type==0){
				$commission_fee =$commission_totalfee*$choufee_set['rate']/100;
				$commission_rate=$choufee_set['rate'];
			}elseif($commission_type==1){
				$commission_fee =$choufee_set['rateyuan'];
				$commission_rate=$choufee_set['rateyuan'];
			}elseif($commission_type==2){
				$minrate=20;
				foreach($choufee_set['ratejieti'] as $rv){
					if(!$rv['rate'] || $rv['fee']>$commission_totalfee){
						continue;
					}
					$minrate=$rv['rate']>$minrate?$minrate:$rv['rate'];
				}
				$commission_fee =$commission_totalfee*$minrate/100;
				$commission_rate=$minrate;
			}elseif($commission_type==3){
				$commission_fee =$commission_totalfee*$choufee_set['rate']/100;
				//$commission_fee =$commission_totalfee*$mv['choufee_set']['payrate']/100;
				$commission_rate=$choufee_set['rate'];
			}elseif($commission_type==4){
				$commission_fee =$order['price']-$mv['costprice'];
				$commission_rate=0;
			}
			if($add && !$merchorder['id']){
				$data=array(
					'uniacid'=>$_SESSION['uniacid'],
					'mid'=>$order['mid'],
					'order_id'=>$order['id'],
					'merchid'=>$mv['id'],
					'order_price'=>$order['price'],
					'sys_peiprice'=>$sys_peiprice>0?$sys_peiprice:$order['dispatchprice'],
					'commission_type'=>$commission_type,
					'commission_price'=>$commission_totalfee,
					'commission_money'=>$commission_fee>0?$commission_fee:0,
					'isdish'=>$order['isdish'],
					'status'=>$order['status']==3?1:0,
					'createtime'=>$order['createtime'],
				);
				$this->pdo_insert('order',$data);
			}elseif($merchorder['id']){
				$data=array(
					'commission_money'=>$commission_fee>0?$commission_fee:0,
				);
				if($merchorder['sys_peiprice']<=0){
					$data['sys_peiprice']=$sys_peiprice>0?$sys_peiprice:$order['dispatchprice'];
				}
				pdo_update('jy_merch_order',$data,array('uniacid'=>$_SESSION['uniacid'],'id'=>$merchorder['id']));
			}
		}
		//返回最后一个商户的
		$merchorder=array(
			'status'=>0,
			'commission_type'=>$commission_type,
			'commission_price'=>$commission_totalfee,
			'allgoodsfee'=>$goodsfee,
			'commission_money'=>$commission_fee,
			'commission_rate'=>$commission_rate,
		);
		return $merchorder;
	}
	function get_chourate($merchid=0){
		$choufee_set=$this->setting;
		$merchsfee=array();
		if($merchid){
				$merch=array();
				if($choufee_set['commission_type']==3){
					$merch=pdo_get('jy_merch_user',array('uniacid'=>$_SESSION['uniacid'],'id'=>$merchid),array('id','choufee_set'));
				}
				$merchsfee[]=array(
					'id'=>$merchid,
					'choufee_set'=>$merch['choufee_set']?json_decode($merch['choufee_set'],true):array(),
				);
		}
		$merchnum=count($merchsfee);
		foreach($merchsfee as &$mv){
			//$mv['dispatchprice']=$mv['dispatchprice']*1/$merchnum;
			$commission_totalfee=0;
			$commission_type=$choufee_set['shopcommission_type'];
			if($commission_type==3 && $mv['choufee_set']){
				$choufee_set=$mv['choufee_set'];
				$commission_type=$choufee_set['shopcommission_type'];
			}
			$choufee_set['rate']=$choufee_set['shoprate'];
			$choufee_set['rateyuan']=$choufee_set['shoprateyuan'];
			$choufee_set['ratejieti']=$choufee_set['shopratejieti'];
			if($commission_type==0){
				$commission_rate=$choufee_set['rate'];
			}elseif($commission_type==1){
				$commission_rate=$choufee_set['rateyuan'];
			}elseif($commission_type==2){
				$minrate=20;
				foreach($choufee_set['ratejieti'] as $rv){
					if(!$rv['rate'] || $rv['fee']>$commission_totalfee){
						continue;
					}
					$minrate=$rv['rate']>$minrate?$minrate:$rv['rate'];
				}
				$commission_rate=$minrate;
			}elseif($commission_type==3){
				$commission_rate=$choufee_set['rate'];
			}elseif($commission_type==4){
				$commission_rate=0;
			}
		}
		//返回最后一个商户的
		$merchorder=array(
			'status'=>0,
			'commission_type'=>$commission_type,
			'commission_price'=>$commission_totalfee,
			'allgoodsfee'=>$goodsfee,
			'commission_money'=>$commission_fee,
			'commission_rate'=>$commission_rate,
		);
		return $merchorder;
	}
	function up_orderstatus($order_id=0,$merchid=0){
		$merchorder=pdo_get('jy_merch_order',array('uniacid'=>$_SESSION['uniacid'],'order_id'=>$order_id));
		if(!$merchorder['id']){
			$this->get_choufee($order_id,true,$merchid);
			return true;
		}
		$order=pdo_get('jy_weishop_order',array('uniacid'=>$_SESSION['uniacid'],'id'=>$order_id));
		if($order['status']==3){
			pdo_update('jy_merch_order',array('status'=>1),array('uniacid'=>$_SESSION['uniacid'],'order_id'=>$order_id));
		}
		return true;
	}
	/*
		根据订单生成佣金
	*/
	function get_zhaoshangorder($order){
		$dispatchprice=$order['dispatchprice'];
		$myzhan_id=pdo_getcolumn('jy_merch_zhaoshanglist',array('uniacid'=>$_SESSION['uniacid'],'mid'=>$order['mid']),'id');
		if($myzhan_id){
			$agent_update=array(
					'self_order_price +='=>$order['price'],
					'self_order_num +='=>1,
			);
			$this->update('zhaoshanglist',$agent_update,array('id'=>$myzhan_id));
			$this->model('zhaoshang')->update_agent_level($myzhan_id);
		}
		if($dispatchprice){
			$dispatchprice=$dispatchprice/count($order['goods']);
		}
		//现在支付方式分佣
		if($this->zhaoshang['pay_type']){
			$paylog=pdo_get('core_paylog',array('id'=>$order['pay_id']),array('pay_type','di_credit','di_fee','fee'));
			if($paylog['di_fee']>0){
				if(strpos($this->zhaoshang['pay_type'],';'.$paylog['di_credit'].';') !== false){
					$this->order['price'] -= $paylog['di_fee'];
				}
				if(strpos($this->zhaoshang['pay_type'],';'.$order['paytype'].';') !== false){
					$this->order['price'] -= $paylog['fee']-$paylog['di_fee'];
				}
				//两种支付都不支持
				if(strpos($this->zhaoshang['pay_type'],';'.$paylog['di_credit'].';') !== false && strpos($this->zhaoshang['pay_type'],';'.$order['paytype'].';') !== false){
					return;
				}
			}elseif(strpos($this->zhaoshang['pay_type'],';'.$order['paytype'].';') !== false){
				return;
			}
		}
		$api_data=json_decode($order['api_data'],true);
		if($this->zhaoshang['feetype']){
			if(!$order['merchid'] && $api_data['dhmerchid']){
				$order['merchid']=$api_data['dhmerchid'];
				$zhaoshangid=pdo_getcolumn('jy_merch_user',array('uniacid'=>$_SESSION['uniacid'],'id'=>$order['merchid']),'zhaoshangid');
			}
		}else{
			$zhaoshangid=pdo_getcolumn('jy_merch_user',array('uniacid'=>$_SESSION['uniacid'],'id'=>$order['merchid']),'zhaoshangid');
		}
		if($zhaoshangid){
			$this->create_commission($zhaoshangid,$order,$dispatchprice);
		}
		return true;
	}
	/*
		给指定招商员计算分红
	*/
	function create_commission($zhaoshangid,$order,$dispatchprice=0){
			$zhaoshang=pdo_get('jy_merch_zhaoshanglist',array('id'=>$zhaoshangid,'uniacid'=>$_SESSION['uniacid']));
			if(!$zhaoshang['level_id']){
				//获取默认等级分红比例
				$level=$this->plugin_setting('default_level');
				$level_id=0;
			}else{
				$level=$this->get('level',array('id'=>$zhaoshang['level_id'],'uniacid'=>$_SESSION['uniacid']));
				$level_id=$level['id'];
			}
			$levelcommission_per=$level['rate'];
			$order_price=$order['price'];
			$money=0;
			$total_lastcostprice=0;
			//非按笔计算
			$this->needorder=false;
			foreach($order['goods'] as $goods){
				$total_price=$goods['total_price']?$goods['total_price']:pdo_getcolumn('jy_weishop_order_goods',array('id'=>$goods['id'],'uniacid'=>$_SESSION['uniacid']),'total_price');
				$shopgoods=pdo_get('jy_weishop_goods',array('id'=>$goods['goodsid'],'uniacid'=>$_SESSION['uniacid']),array('pcate','costprice'));
				$costprice=$shopgoods['costprice'];
				$total_costprice=$goods['total']*$costprice;
				$total_lastcostprice +=$total_costprice;
				
				//获取默认等级佣金比例
				$zhaoshang['level_id']=$zhaoshang['level_id']?:0;
				$commissioninfo=$this->get('goods',array('goods_id'=>$goods['goodsid'],'uniacid'=>$_SESSION['uniacid']));
				if(!$commissioninfo['id']){
					$this->needorder=true;
					continue;
				}
				if($commissioninfo['ignore']){
					$order_price=$order_price-$total_price;
					continue;
				}
				$commissioninfo['commissions']=json_decode($commissioninfo['commissions'],true);
				$commission_per=$commissioninfo['commissions']['commission'.$zhaoshang['level_id']];
				if($commissioninfo['commissiontype']){
						if($commissioninfo['commissiontype']==1){
							$commission_money=($total_price*$commission_per)/100;
						}elseif($commissioninfo['commissiontype']==3){
							$commission_money=(($total_price-$dispatchprice)*$commission_per)/100;
						}else{
							$commission_money=(($total_price-$total_costprice)*$commission_per)/100;
						}
				}else{
						if($this->zhaoshang['commissiontype']==1){
							$commission_money=($total_price*$commission_per)/100;
						}elseif($this->zhaoshang['commissiontype']==3){
							$commission_money=(($total_price-$dispatchprice)*$commission_per)/100;
						}else{					
							$commission_money=(($total_price-$total_costprice)*$commission_per)/100;
						}
				}
				$money+=$commission_money;
				$remark=array(
					'level_id'=>$zhaoshang['level_id'],
					'order_price'=>$total_price,
					'commission_money'=>$commission_money,
					'commission_per'=>$commission_per,
					'goods_commission'=>array(
						array('goods_id'=>$goods['goodsid'],'total_price'=>$total_price,'total'=>$goods['total'],'commission_per'=>$commission_per,'commission_money'=>$commission_money),
					),
				);
			}
			if($this->needorder || count($order['goods'])<=0){
				if($this->zhaoshang['commissiontype']==1){
					$feetotal_price=$order_price;
				}elseif($this->zhaoshang['commissiontype']==3){
					$feetotal_price=$order_price-$dispatchprice;
				}else{					
					$feetotal_price=$order_price-$total_lastcostprice;
				}
				if(substr($levelcommission_per,-1)=='%'){
					$commission_money=($feetotal_price*substr($levelcommission_per,0,-1))/100;
				}else{
					$commission_money=$levelcommission_per;
				}
				$money+=$commission_money;
			}
			if($money>0){
				$new_commission=array(
					'uniacid'=>$_SESSION['uniacid'],
					'mid'=>$zhaoshang['mid'],
					'zhaoshangid'=>$zhaoshangid,
					'area_code'=>$zhaoshang['code'],
					'level'=>$zhaoshang['level_id'],
					'orderid'=>$order['id'],
					'codepay'=>$order['codepay'],
					'money'=>price($money),
					'remark'=>ijson_encode($remark),
					'commission_type'=>$commissioninfo['commissiontype']?:$this->zhaoshang['commissiontype'],
					'createtime'=>TIMESTAMP,
					'status'=>0,
				);
				$this->insert('commission',$new_commission);
			}
			$agent_update=array(
				'order_price +='=>$order['price'],
				'order_num +='=>1,
				'commission_money +='=>$money,
			);
			$this->update('zhaoshanglist',$agent_update,array('id'=>$zhaoshang['id']));
			$this->model('zhaoshang')->update_agent_level($zhaoshangid);
	}
	/*
		商户引流抽成
	*/
	function get_fans_choufee($order){
		$rate=intval($this->setting['fans_choufee']);
		if(!$rate){
			return false;
		}
		$check=pdo_get('jy_merch_member',array('merchid'=>$order['merchid'],'mid'=>$order['mid'],'uniacid'=>$_SESSION['uniacid']));
		if(!$check['fmerchid']){
			return false;
		}
		$money=$rate*$order['price']/100;
		if($money>0){
				$new_commission=array(
					'uniacid'=>$_SESSION['uniacid'],
					'koumerchid'=>$order['merchid'],
					'merchid'=>$check['fmerchid'],
					'type'=>2,
					'orderid'=>$order['id'],
					'codepay'=>$order['codepay'],
					'money'=>price($money),
					'createtime'=>TIMESTAMP,
					'status'=>0,
				);
				$this->insert('commission',$new_commission);
		}
		return true;
	}
	/*
		营销活动的佣金情况
	*/
	public function up_salefee($goods=array(),$plugin=''){
		if($plugin=='jy_pingdd_pin'){
			if($goods['open_sale'] && $goods['sale_fee'] && $goods['merchid']){
				$this->model('member')->addcreditlog($goods['merchid'],0,$goods['sale_fee'],2,1,'分销拼团任务活动收益');
			}
		}
		return true;
	}
	/*
		订单完成时，把佣金状态由初始的订单待完成升级为待结算
	*/
	public function order_complete($order=array()){
		if($order['id']){
			$params=array('uniacid'=>$_SESSION['uniacid'],'orderid'=>$order['id']);
			if($order['codepay']){
				$params['codepay']=1;
			}else{
				$params['codepay']=0;
			}
			$this->update('commission',array('status'=>1,'complete_time'=>TIMESTAMP),$params);
		}
	}
	/*
		创建结算单
	*/
	public function create_balance($mid,$post=array()){
		if(!$mid){
			exi('请先选择要结算的会员！','error');
		}
		if($post['pay_type']=='downline'){
			if(!$post['wechat']){
				exi('请先填写微信号！','error');
			}
			if(!$post['mobile']){
				exi('请先填写联系电话！','error');
			}
		}
		if($post['pay_type']=='alipay'){
			if(!$post['realname']){
				exi('请先填写真实姓名！','error');
			}
			if(!$post['alipay']){
				exi('请先填写支付宝帐号！','error');
			}
			if(!$post['alipay1']){
				exi('请确认帐号','error');
			}
			if($post['alipay1']!=$post['alipay']){
				exi('确认帐号与支付宝帐号不匹配！','error');
			}
			$typetitle='支付宝';
		}
		if($post['pay_type']=='union'){
			if(!$post['realname']){
				exi('请输入姓名','error');
			}
			if(!$post['bankname']){
				exi('请输入所属银行','error');
			}
			if(!$post['bankcard']){
				exi('请银行卡号','error');
			}
			if(!$post['bankcard1']){
				exi('请确认卡号','error');
			}
			if($post['bankcard']!=$post['bankcard1']){
				exi('确认卡号与银行卡号不匹配！','error');
			}
			$typetitle='银行卡';
		}
		//获取可结算佣金
		$params=array('uniacid'=>$_SESSION['uniacid'],'mid'=>$mid,'status'=>1);
		if($this->zhaoshang['balance_day']){
			//结算天数
			$time=TIMESTAMP-intval($this->zhaoshang['balance_day'])*86400;
			$params['complete_time <']=$time;
		}
		$commissions=$this->getall('commission',$params);
		if(!$commissions){
			exi('没有可结算的佣金！','error');
		}
		$commission_num=count($commissions);
		$money=0;
		foreach($commissions as $commission){
			$money+=$commission['money'];
		}
		//结算最低佣金检测
		if($money<1){
			exi('可结算佣金太少，无法结算！','error');
		}
		$new_balance=array(
			'uniacid'=>$_SESSION['uniacid'],
			'mid'=>$mid,
			'balancesn'=>get_ordersn($this->tablename('commission'),'balancesn','AC'),
			'commission_num'=>$commission_num,
			'money'=>$money,
			'pay_type'=>$post['pay_type'],
			'realname'=>$post['realname'],
			'alipay'=>$post['alipay'],
			'bankname'=>$post['bankname'],
			'bankcard'=>$post['bankcard'],
			'wechat'=>$post['wechat'],
			'createtime'=>TIMESTAMP,
			'status'=>0,
		);
		//结算手续费
		if($this->zhaoshang['balance_per']){
			$new_balance['balance_pay']=intval($new_balance['money']*$this->zhaoshang['balance_per'])/100;
		}else{
			$new_balance['balance_pay']=0;
		}
		$new_balance['final_money']=$new_balance['money']-$new_balance['balance_pay'];
		$this->insert('balance',$new_balance);
		$balance_id=pdo_insertid();
		foreach($commissions as $commission){
			$update=array(
				'status'=>2,
				'balancesn'=>$new_balance['balancesn'],
			);
			$this->update('commission',$update,array('id'=>$commission['id']));
		}
		return $balance_id;
	}
	/*
		审核结算单
	*/
	public function check_balance($balance_id,$status,$remark=''){
		$update=array(
			'status'=>$status,
			'check_time'=>TIMESTAMP,
			'remark'=>$remark,
		);
		$this->update('balance',$update,array('id'=>$balance_id));
	}
	/*
		打款结算单
	*/
	public function pay_balance($balance_id,$pay_type){
		$balance=$this->get('balance',array('id'=>$balance_id,'uniacid'=>$_SESSION['uniacid']));
		if(!$balance || $balance['status']!=1){
			exi('结算单不存在或者不符合打款条件，打款失败！','error');
		}
		$pay_types=array_keys($this->pay_types);
		if(!$pay_type || !in_array($pay_type,$pay_types)){
			exi('未设置打款方式或者打款方式不合法，打款失败！','error');
		}
		$func_name='pay_balance_by_'.$pay_type;
		$trade_no=$this->$func_name($balance['mid'],$balance['final_money'],$balance['balancesn']);
		$update=array(
			'status'=>3,
			'pay_type'=>$pay_type,
			'pay_trade_no'=>$trade_no,
			'pay_time'=>TIMESTAMP,
		);
		$this->update('balance',$update,array('id'=>$balance['id']));
		return true;
	}
	/*
		订单结算
	*/
	public function pay_clearing($clearing_id,$pay_type){
		$clearing=$this->get('clearing',array('id'=>$clearing_id,'uniacid'=>$_SESSION['uniacid']));
		$clearing['mid']=pdo_getcolumn('jy_merch_user',array('uniacid'=>$_SESSION['uniacid'],'id'=>$clearing['merchid']),'mid');
		if(!$clearing || $clearing['status']!=1){
			exi('结算单不存在或者不符合打款条件，打款失败！','error');
		}
		$pay_type=$pay_type?:$clearing['pay_type'];
		$pay_types=array_keys($this->pay_types);
		if(!$pay_type || !in_array($pay_type,$pay_types)){
			//exi('未设置打款方式或者打款方式不合法，打款失败！','error');
			$update=array();
			if($clearing['finalprice']>0){
				$update['credit2 +=']=$clearing['finalprice'];
			}
			if($clearing['fdelcredit1']>0){
				$update['credit1 +=']=$clearing['fdelcredit1'];
			}
			if($update){
				pdo_update('jy_merch_user',$update,array('uniacid'=>$_SESSION['uniacid'],'id'=>$clearing['merchid']));
			}
			if($clearing['finalprice']>0){
				$feelog=array(
					'mid'=>$clearing['mid'],
					'merchid'=>$clearing['merchid'],
					'msg'=>'结算进商户钱包',
					'gift_type'=>0,
					'fee'=>$clearing['finalprice'],
					'status'=>0,
				);
				$this->model('model')->set_feelog($feelog,2);
			}
			if($clearing['fdelcredit1']>0){
				$feelog=array(
					'mid'=>$clearing['mid'],
					'merchid'=>$clearing['merchid'],
					'msg'=>'结算获得积分',
					'gift_type'=>0,
					'fee'=>$clearing['fdelcredit1'],
					'status'=>0,
				);
				$this->model('model')->set_feelog($feelog,4);
			}
		}else{
			$func_name='pay_balance_by_'.$pay_type;
			$trade_no=$this->$func_name($clearing['mid'],$clearing['finalprice'],$clearing['clearno'],$clearing['realname']);
		}
		$update=array(
			'status'=>2,
			'pay_type'=>$pay_type,
			'pay_trade_no'=>$trade_no,
			'pay_time'=>TIMESTAMP,
		);
		$this->update('clearing',$update,array('uniacid'=>$_SESSION['uniacid'],'id'=>$clearing['id']));
		if($clearing['jifen']>0){
			cfc('core')->member_credit($clearing['mid'],'credit1',$clearing['jifen'],true,'jy_merch','商户提现送积分');
		}
		$this->model('notice')->notice_fenxiao_dakuan($clearing['mid'],$clearing['finalprice']);
		return true;
	}
	/*
		打款到余额
	*/
	protected function pay_balance_by_credit($mid,$money,$balancesn){
		return cfc('core')->member_credit($mid,'credit2',$money,true,$this->plugin_name,$remark='分销佣金打款');
	}
	/*
		打款到微信零钱
	*/
	protected function pay_balance_by_wechat($mid,$money,$balancesn='',$realname=''){
		$params=array(
			'openid'=>$mid,
			'mch_billno'=>$balancesn,
			'money'=>$money,
			'remark'=>'代理商佣金发放',
			'realname'=>$realname,
		);
		$res=cfc('wechat_pay')->mch_pay($params);
		return $res['payment_no'];
	}
	/*
		打款到支付宝
	*/
	protected function pay_balance_by_alipay($mid,$money,$balancesn){
		return 'pay_trade_no';
		exi('暂不支持打款到支付宝！','error');
	}
	/*
		打款到银行账户
	*/
	protected function pay_balance_by_union($mid,$money,$balancesn){
		return 'pay_trade_no';
		exi('暂不支持打款到银行账户！','error');
	}

	public function commission_remark($remark){
		if(!$remark){
			return '';
		}
		$remark=json_decode($remark,true);
		$goods_str='';
		if($remark['goods_commission']){
			foreach($remark['goods_commission'] as $goods){
				$goods['title']=pdo_getcolumn('jy_weishop_goods',array('id'=>$goods['goods_id']),'title');
				$goods_str.="商品ID【{$goods['goods_id']}】，商品名称【{$goods['title']}】，商品数量【{$goods['total']}】，商品总价【{$goods['total_price']}】，佣金比例【{$goods['commission_per']}%】，分销佣金【{$goods['commission_money']}】\n";
			}
		}else{
			$goods_str='未启用';
		}
		$remark['level_title']=pdo_getcolumn('jy_merch_level',array('id'=>$remark['level_id']),'title');
		$remark['level_title']=$remark['level_title']?:'默认等级';
		$remark_str=<<<TPL
招商员等级ID【{$remark['level_id']}】，等级名称【{$remark['level_title']}】，订单总价【{$remark['order_price']}】
商品独立佣金：
{$goods_str}
佣金总计：【{$remark['commission_money']}】
TPL;
		return $remark_str;
	}
}